import { Component, OnInit } from '@angular/core';
import {TalentService} from '../../../service/talent.service';
import {SnackSuccessComponent} from '../../snack-success/snack-success.component';
import {SnackService} from '../../../service/snack.service';
import {MatDialog, MatSnackBar} from '@angular/material';
import {SnackErrorComponent} from '../../snack-error/snack-error.component';
import {DelDialogComponent} from '../del-dialog/del-dialog.component';

@Component({
  selector: 'app-application-results',
  templateUrl: './application-results.component.html',
  styleUrls: ['./application-results.component.css']
})
export class ApplicationResultsComponent implements OnInit {
  appList: any = new Array();
  appAllList: any;
  status = 0;
  constructor(
    public talentService: TalentService,
    public snackBar: MatSnackBar,
    public snackService: SnackService,
    public dialog: MatDialog
  ) {
    this.loadData();
  }

  ngOnInit() {
  }

  loadData() {
    this.talentService.appList()
      .subscribe(list => {
        this.appList = list;
        this.appAllList = list;
      });
  }

  delApp(id: number, resumeId: number) {
    this.talentService.delApp(id, resumeId)
      .subscribe(re => {
        this.snackService.message = '删除成功';
        this.loadData();
        this.snackBar.openFromComponent(SnackSuccessComponent, {
          duration: 3000,
        });
      }, err => {
        console.error('ERROR', err);
        this.snackService.message = err.error.message;
        this.snackBar.openFromComponent(SnackErrorComponent, {
          duration: 3000,
        });
      });
  }

  openDialog(id: number,  resumeId: number): void {
    let dialogRef = this.dialog.open(DelDialogComponent, {
      width: '250px',
    });

    dialogRef.afterClosed().subscribe(result => {
      if (result == 'next') {
        this.delApp(id, resumeId);
      }
    });
  }

  statusFlag(status: number) {
    if (status != 0 ) {
      let newList = new Array();
      this.appAllList.forEach(function (e) {
        if (e.status == status) {
          newList.push(e);
        }
      })
      this.appList = newList;
    } else {
      this.appList = this.appAllList;
    }
    this.status = status;
  }
}
